<template>
  <UserSelectorModal ref="UserSelectorModal" @selectedUsers="selectedUsers"/>
</template>

<script>
import UserSelectorModal from '@/components/UserSelectorModal';
import {guid} from '@/utils/CommonUtils';

export default {
  name: "UserTaskModal",
  components: {
    UserSelectorModal
  },
  data() {
    return {
      userTask: {
        name: undefined,
        userConfigs: []
      }
    };
  },
  methods: {
    setData(userTask) {
      this.initPage();
      this.resolveTaskDefaultValue(userTask);
      this.userTask = JSON.parse(JSON.stringify(userTask));
      this.$refs.UserSelectorModal.setData(this.userTask.userConfigs);
    },
    resolveTaskDefaultValue(userTask) {
      if (!userTask.userConfigs) userTask.userConfigs = [];
    },
    selectedUsers() {
      this.userTask.id = guid();
      this.resolveUserTaskName();
      this.$emit('addUserTask', this.userTask);
      this.$refs.UserSelectorModal.close();
    },
    resolveUserTaskName() {
      let res = this.userTask.userConfigs.map(c => c.configName);
      res = res.join(',');
      this.userTask.name = res;
    },
    initPage() {
      this.userTask = {
        name: undefined,
        userConfigs: []
      };
    },
  }
};
</script>
